﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Qianke.Core.Common
{
    public static class TokenHelper
    {

        private static string tokenKey = "*&^%$#@#$%^&!@#$%&)(*&^%             ";

        /// <summary>
        /// 创建Token
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string CreateToken(object model)
        {
            return CreateToken(model, tokenKey);
        }

        public static string CreateToken<T>(T model, string key)
        {
            return AESHelper.EncryptToByteString(Encoding.UTF8.GetBytes(model.ToJson()), key, null, Encoding.UTF8);
        }


        /// <summary>
        /// 主要用于接口 或者 ajax  因为这两种调用方式 没有cookie
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strToken"></param>
        /// <returns></returns>
        public static T TokenToModel<T>(string strToken)
        {
            try
            {
                return TokenDecode(strToken).ToObject<T>();
            }
            catch
            {
                return default(T);
            }
        }

        /// AES解密(无向量)  
        /// </summary>  
        /// <param name="encryptedBytes">被加密的明文</param>  
        /// <param name="key">密钥</param>  
        /// <returns>明文</returns>  
        public static string Decrypt(string data, string key)
        {
            Byte[] encryptedBytes = BytesEncryptHelper.DecodeBytes(data);
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            using (var aes = Aes.Create())
            {
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                aes.Key = bKey;

                using (var mStream = new MemoryStream(encryptedBytes))
                {
                    using (var cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        byte[] tmp = new byte[encryptedBytes.Length + 32];
                        int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
                        byte[] ret = new byte[len];
                        Array.Copy(tmp, 0, ret, 0, len);
                        return Encoding.UTF8.GetString(ret);
                    }
                }
            }
        }


        public static string TokenDecode(string strToken)
        {
            //return Decrypt(strToken, tokenKey);
            return AESHelper.DecryptFromByteString(strToken, tokenKey, null, Encoding.UTF8, Encoding.UTF8);
        }

        public static object TokenToModel(string strToken,Type dataType)
        {
            try
            {
                return TokenDecode(strToken).ToObject(dataType);
            }
            catch
            {
                return null;
            }
        }

    }
}
